package com.cloud.pole.shop.controller.order.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.shop.biz.order.ShopOrderBiz;
import com.cloud.pole.shop.model.entity.order.ShopOrder;
import com.cloud.pole.shop.model.vo.order.ShopOrderRefundAdminVo;
import com.cloud.pole.shop.service.order.OrderRefundService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 商家管理退单
 * @author ThinkPad
 *
 */
@RestController
@RequestMapping("/order/refund/admin_merch")
@Slf4j
public class OrderRefundAdminMerchantController extends BaseController<ShopOrderBiz,ShopOrder>{
	
	@Autowired
    private OrderRefundService orderRefundService;
	
	/**
	 * 分页查询退单
	 * @param pageNo
	 * @param pageSize
	 * @param mobile
	 * @param templateName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
    @ResponseBody
    public ApiRestResponse<ShopOrderRefundAdminVo> list(
    		@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
    		@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
    		@RequestParam(name="searchType", defaultValue="") String searchType,
    		@RequestParam(name="orderSn", defaultValue="") String orderSn,
    		@RequestParam(name="refundId", defaultValue="") String refundId,
    		@RequestParam(name="refundWay", defaultValue="") String refundWay,
    		@RequestParam(name="refundStatus", defaultValue="") String refundStatus,
    		@RequestParam(name="startTime", defaultValue="") String startTime,
    		@RequestParam(name="endTime", defaultValue="") String endTime){
		try {
			Map<String, String> params = new HashMap<>();
			params.put("searchType", searchType);
			params.put("orderSn", orderSn);
			params.put("refundId", refundId);
			params.put("refundWay", refundWay);
			params.put("refundStatus", refundStatus);
			params.put("startTime", startTime);
			params.put("endTime", endTime);
			PageInfo<ShopOrderRefundAdminVo> pageInfo = orderRefundService.selectPageShopOrderRefundAdminList(params, pageNo, pageSize);
			return ApiRestResponse.successOfPage("成功", (int)pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageSize(),
					pageInfo.getList());
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
    }
	
	@RequestMapping(value = "/getOrderRefundInfo/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ApiRestResponse<ShopOrderRefundAdminVo> getOrderRefundInfo(@PathVariable String id){
		try {
			ShopOrderRefundAdminVo shopOrder = null;
			String msg = "成功";
			if(id != null){
				shopOrder = orderRefundService.selectOrderRefundAdminById(id);
			}else{
				msg = "退单编号不能为空";
			}
			return ApiRestResponse.success(msg, shopOrder);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
    }
}
